package com.haidung.photojourney.views;

import java.util.ArrayList;

import org.w3c.dom.Document;

import com.androidquery.AQuery;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolylineOptions;
import com.haidung.photojourney.R;
import com.haidung.photojourney.events.Event;
import com.haidung.photojourney.events.EventListener;
import com.haidung.photojourney.events.SimpleEvent;
import com.haidung.photojourney.models.GoogleMapModel;
import com.haidung.photojourney.vos.JourneyMarker;
import com.twotoasters.clusterkraf.Clusterkraf;
import com.twotoasters.clusterkraf.InputPoint;
import com.twotoasters.clusterkraf.Options.ClusterClickBehavior;

import android.content.Context;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;

public class MainView extends RelativeLayout {
    /* The interface to send events from the view to the controller */
    public static interface ViewListener {
        public void showImageLocation();

        public GoogleMap initMap();
    }

    private GoogleMapModel model;
    AQuery aq;
    private GoogleMap googleMap;
    private ViewListener viewListener;// The listener reference for sending events

    public void setViewListener(ViewListener viewListener) {
        this.viewListener = viewListener;
    }

    public MainView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    // Remove the listener from the model
    public void destroy() {

    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        initView();
    }

    private void initView() {
        aq = new AQuery(this);
        aq.id(R.id.btn).clicked(this, "buttonClicked");
        model = GoogleMapModel.getInstance();
        model.addListener(GoogleMapModel.MapEvent.ADD_MARKER, addMarkerListener);
        model.addListener(GoogleMapModel.MapEvent.INIT_MAP, initMapListener);
    }

    public void buttonClicked(View view) {
        viewListener.showImageLocation();
    }

    /*
     * The listener for when the property changes on the model
     */
    private EventListener addMarkerListener = new EventListener() {
        @Override
        public void onEvent(Event event) {
            if (googleMap == null) {
                initMap();
            }

            JourneyMarker marker = (JourneyMarker) ((SimpleEvent) event).getValue();
            aq.id(R.id.longtitue).text("longtitue: " + marker.getMarker().getPosition().longitude);
            aq.id(R.id.latitue).text("latitue: " + marker.getMarker().getPosition().latitude);
            googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(marker.getMarker().getPosition(), 5));

        }

        private void initMap() {

        }
    };

    private EventListener initMapListener = new EventListener() {

        @Override
        public void onEvent(Event event) {
            // Loading map
            if (googleMap == null) {
                googleMap = viewListener.initMap();
                if (googleMap != null) {
                    googleMap.setOnMarkerClickListener(new OnMarkerClickListener() {

                        @Override
                        public boolean onMarkerClick(Marker arg0) {

                            return false;
                        }
                    });
                }
            }
        }
    };
}
